<template>
  <div class="goodsFenleiCreate">
    <el-card class="box-card">
      <!-- 头部 -->
      <div slot="header" class="clearfix header">
        <span>创建商品类型</span>
        <el-button type="primary" round @click="backFn">返回</el-button>
      </div>
      <!-- end---------------- -->

      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input
            type="text"
            autocomplete="off"
            v-model="ruleForm.cat_name"
          ></el-input>
        </el-form-item>

        <el-form-item label="所属类型" prop="parent_id">
          <el-select placeholder="请选择" v-model="ruleForm.parent_id">
            <!-- <el-cascader :props="props" :options="options" @change="handleChange" change-on-select
                            clearable>
                        </el-cascader> -->
            <el-option
              v-for="item in options"
              :key="item.cat_id"
              :label="item.cat_name"
              :value="item.cat_id"
            >
            </el-option>
          </el-select>
          <p style="font-size: 10px; color: #ccc; margin-bottom: -20px">
            备注：商品分类 一级/二级 先URL 再关键词 三级 先URL 再分类ID
          </p>
        </el-form-item>

        <el-form-item label="关键词" prop="keywords">
          <el-input
            type="text"
            v-model="ruleForm.keywords"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="跳转" prop="url">
          <el-input
            type="text"
            v-model="ruleForm.url"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <!-- 按钮 -->
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
        <!-- ------------------------------end -->
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getFenleiIndex, postFenleiCreate } from "@/api";
export default {
  created() {
    getFenleiIndex({
      pagenum: 1,
      pagesize: 100
    }).then(res => {
      // console.log(res);
      this.options = res.data.list;
    });
  },

  data() {
    return {
      options: [],
      props: {
        label: "cat_name",
        value: "cat_id"
      },
      //表单添加数据
      ruleForm: {
        cat_name: "",
        cat_id: "",
        url: "",
        parent_id: []
      },
      rules: {
        //键 ruleForm里面的键
        //值 数组里面是一个个对象 赋值
        cat_name: [
          { required: true, message: "请输入类型名称", trigger: "blur" }
          // { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ]
      }
    };
  },
  methods: {
    //调用页面数据 获取到list的值将其给options，去渲染到请选择的框里

    handleChange(value) {
      // console.log(value);
    },

    submitForm(refName) {
      //this.$refs[formName].validate((valid) => {}
      this.$refs[refName].validate(isSuccess => {
        if (isSuccess) {
          // this.$message.success("发送异步请求");
          postFenleiCreate(this.ruleForm).then(res => {
            // console.log(res)
            if (res.meta.state == 201) {
              this.$router.push({ path: "/goods/cate" });
            }
          });
        }
      });
    },
    resetForm(refName) {
      //  this.$refs[formName].resetFields();
      this.$refs[refName].resetFields();
    },

    backFn() {
      this.$router.push({ path: "/goods/cate" });
    }
  }
};
</script>

<style lang="scss" scoped>
.goodsFenleiCreate {
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>
